const db = require("../db/index");

const operationLogController = {
  // 操作记录
  operationLog: async (req, res) => {
    const { operation_person, operation_content, operation_level } = req.body;
    const operation_time = new Date();
    const sql = "INSERT INTO operation_log SET ?";
    try {
      await db.query(sql, {
        operation_person,
        operation_content,
        operation_level,
        operation_time,
      });
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取操作记录列表
  operationLogList: async (req, res) => {
    const { page } = req.query;
    const sql = `SELECT * FROM operation_log ORDER BY operation_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM operation_log`;
    try {
      const [data] = await db.query(sql);
      const [total] = await db.query(sql1);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 搜索最近十条操作记录列表
  searchoperationLogList: async (req, res) => {
    const { account } = req.query;
    const sql = `SELECT * FROM operation_log WHERE account = ? LIMIT 10 OFFSET 0`;
    try {
      const [data] = await db.query(sql, account);
      res.send({
        status: 1,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 清空操作日志
  clearoperationLog: async (req, res) => {
    const sql = "TRUNCATE TABLE operation_log";
    try {
      await db.query(sql);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
};

module.exports = operationLogController;
